import time
import urllib.parse
import allure
import pytest
import requests
import os
basedir=os.path.dirname(__file__)
filepath=os.path.join(basedir,"testCase/updateDoctor.json")
from .data import load_updateDoctordata,baseurl
@allure.epic("修改患者医生")
class TestUpdateDoctor:
    def setup_class(self):
        self.session = requests.session()
        self.url=f"{baseurl}/patients/update_doctor"
        print("修改患者医生接口测试开始：", time.strftime("%Y-%m-%d-%H:%M:%S"))

    def teardown_class(self):
        self.session.close()
        print("修改患者医生接口测试结束：", time.strftime("%Y-%m-%d-%H:%M:%S"))
    @allure.story("医生修改")
    @pytest.mark.parametrize("testCase,username,doctor_id,msg,status_code",load_updateDoctordata(filepath))
    @allure.title("{testCase}")
    def test_updateDoctor(self,testCase,username,doctor_id,msg,status_code):
        self.session.post(f"{baseurl}/login",data={"username":"doctor","password":"111111"})
        data={
            "username":username,
            "doctor_id":doctor_id
        }
        responses=self.session.post(self.url,data=data)
        url=urllib.parse.unquote(responses.url)
        assert msg in url
        assert status_code==responses.status_code
        self.session.cookies.clear()
    @allure.story("非医生修改")
    @pytest.mark.parametrize("testCase,username1,password,username2,",[
        ("普通用户修改","user10","111111","test_updateDoctor2"),
        ("管理员修改","manage","111111","test_updateDoctor3"),
        ("超级管理员修改", "root", "111111", "test_updateDoctor3")
    ])
    @allure.title("{testCase}")
    def test_updateDoctorAnother(self,testCase,username1,password,username2):
        self.session.post(f"{baseurl}login",data={"username":username1,"password":password},allow_redirects=False)
        responses=self.session.post(self.url,data={"username":username2,"doctor_id":66})
        assert "login" in responses.url
        assert 200==responses.status_code
        self.session.cookies.clear()